package com.lihx.kvstore.utils;

import com.lihx.kvstore.constants.KVConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * 文件相关工具类
 */
public class FileUtils {

    /**
     * 构建sstable文件全路径
     * @param fileNumber
     * @return
     */
    public final static String buildSstableFilePath(Long fileNumber) {
        return KVConstants.WORK_DIR + fileNumber + KVConstants.FILE_SUFFIX_SSTABLE;
    }

    /**
     * 根据文件全路径解析文件编号
     *
     * @param filePath
     * @return
     */
    public final static Long parseFileNumber(String filePath) {
        if (StringUtils.isBlank(filePath)) {
            return -99L;
        }

        Integer lastIndex = filePath.lastIndexOf('/');
        if (lastIndex == -1) {
            return -99L;
        }

        String fileName = filePath.substring(lastIndex + 1);
        return Long.valueOf(fileName.substring(0, fileName.indexOf('.')));
    }
}
